package com.yic.module.wms.controller.admin.warehouselocation;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.enums.CommonStatusEnum;
import com.yic.module.wms.controller.admin.warehousearea.vo.WarehouseAreaSimpleRespVO;
import com.yic.module.wms.convert.warehousearea.WarehouseAreaConvert;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.module.wms.dal.dataobject.warehousearea.WarehouseAreaDO;
import com.yic.module.wms.service.warehouse.WarehouseService;
import com.yic.module.wms.service.warehousearea.WarehouseAreaService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.warehouselocation.vo.*;
import com.yic.module.wms.dal.dataobject.warehouselocation.WarehouseLocationDO;
import com.yic.module.wms.convert.warehouselocation.WarehouseLocationConvert;
import com.yic.module.wms.service.warehouselocation.WarehouseLocationService;

@Tag(name = "管理后台 - 库位")
@RestController
@RequestMapping("/wms/warehouse-location")
@Validated
public class WarehouseLocationController {

    @Resource
    private WarehouseLocationService warehouseLocationService;
    @Resource
    private WarehouseAreaService warehouseAreaService;
    @Resource
    private WarehouseService warehouseService;

    @PostMapping("/create")
    @Operation(summary = "创建库位")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-location:create')")
    public CommonResult<Long> createWarehouseLocation(@Valid @RequestBody WarehouseLocationCreateReqVO createReqVO) {
        return success(warehouseLocationService.createWarehouseLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库位")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-location:update')")
    public CommonResult<Boolean> updateWarehouseLocation(@Valid @RequestBody WarehouseLocationUpdateReqVO updateReqVO) {
        warehouseLocationService.updateWarehouseLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:warehouse-location:delete')")
    public CommonResult<Boolean> deleteWarehouseLocation(@RequestParam("id") Long id) {
        warehouseLocationService.deleteWarehouseLocation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-location:query')")
    public CommonResult<WarehouseLocationRespVO> getWarehouseLocation(@RequestParam("id") Long id) {
        WarehouseLocationDO warehouseLocation = warehouseLocationService.getWarehouseLocation(id);
        return success(WarehouseLocationConvert.INSTANCE.convert(warehouseLocation));
    }

    @GetMapping("/list")
    @Operation(summary = "获得库位列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-location:query')")
    public CommonResult<List<WarehouseLocationRespVO>> getWarehouseLocationList(@RequestParam("ids") Collection<Long> ids) {
        List<WarehouseLocationDO> list = warehouseLocationService.getWarehouseLocationList(ids);
        return success(WarehouseLocationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库位分页")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-location:query')")
    public CommonResult<PageResult<WarehouseLocationRespVO>> getWarehouseLocationPage(@Valid WarehouseLocationPageReqVO pageVO) {
        PageResult<WarehouseLocationDO> pageResult = warehouseLocationService.getWarehouseLocationPage(pageVO);
        PageResult<WarehouseLocationRespVO> respVO = WarehouseLocationConvert.INSTANCE.convertPage(pageResult);
        respVO.getList().forEach(resp -> {
            // 封装仓库
            WarehouseDO warehouse = warehouseService.getWarehouse(resp.getWarehouseId());
            if (ObjectUtil.isNotEmpty(warehouse)) {
                resp.setWarehouseName(warehouse.getName());
            }
            // 封装库区
            WarehouseAreaDO warehouseArea = warehouseAreaService.getWarehouseArea(resp.getAreaId());
            if (ObjectUtil.isNotEmpty(warehouseArea)) {
                resp.setWarehouseAreaName(warehouseArea.getName());
            }
        });
        return success(respVO);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库位 Excel")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-location:export')")
    @OperateLog(type = EXPORT)
    public void exportWarehouseLocationExcel(@Valid WarehouseLocationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WarehouseLocationDO> list = warehouseLocationService.getWarehouseLocationList(exportReqVO);
        // 导出 Excel
        List<WarehouseLocationExcelVO> datas = WarehouseLocationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "库位.xls", "数据", WarehouseLocationExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取库位精简信息列表", description = "只包含被开启的库位，主要用于前端的下拉选项")
    public CommonResult<List<WarehouseLocationSimpleRespVO>> getSimpleWarehouseLocationList(@RequestParam("areaId") Long areaId) {
        // 获得库区列表，只要开启状态的
        List<WarehouseLocationDO> list = warehouseLocationService.getWarehouseLocationList(null, areaId, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(WarehouseLocationDO::getSort));
        List<WarehouseLocationSimpleRespVO> warehouseLocationSimpleRespVOS = WarehouseLocationConvert.INSTANCE.convertList03(list);
        warehouseLocationSimpleRespVOS.forEach(warehouseLocationSimpleRespVO -> {
            warehouseLocationSimpleRespVO.setIndex("2-" + warehouseLocationSimpleRespVO.getId());
        });
        return success(warehouseLocationSimpleRespVOS);
    }

}
